/**
 * ImageLoaderService.java
 * Copyright 2014-9-3 FengXinZi,all right reserved.
 * FengXinZi PROPRIETARY/CONFIDENTIAL, any from of usage is subject to approval.
 */
/**
 * 
 */
package net.arvin.service;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class ImageLoaderService {

	private static int Mb = 1024 * 1024;
	private static int DISC_CACHE_SIZE = 50 * Mb;
	private static int MEMORY_CACHE_SIZE = 3 * Mb;
	private static int THREAD_POOL_SIZE = 3;

	private static final int SECOND = 1000;
	private static final int SOCKET_TIMEOUT_IN_MILLISECONDS = 5 * SECOND;
	private static final int SOCKET_BUFFER_SIZE = 8 * 1024;

	private final ImageLoader mImageLoader = ImageLoader.getInstance();

	/**
	 * Initialize the ImageLoader object with common configuration.
	 * 
	 * @param context
	 */
	public void initImageLoader(final Context context) {

		final ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context)
				.threadPoolSize(THREAD_POOL_SIZE)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCacheSize(MEMORY_CACHE_SIZE)
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())
				.imageDownloader(
						new BaseImageDownloader(context,
								SOCKET_TIMEOUT_IN_MILLISECONDS,
								SOCKET_BUFFER_SIZE))
				.diskCacheSize(DISC_CACHE_SIZE).diskCacheFileCount(1000)
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs().build();
		// Initialize ImageLoader with configuration.
		mImageLoader.init(config);
	}

	/**
	 * Create a new {@link DisplayImageOptions} object.
	 * 
	 * @return common image display options
	 */
	public DisplayImageOptions defaultDisplayImageOptionsBuilder(
			final int resourceId) {
		return new DisplayImageOptions.Builder().showImageOnLoading(resourceId)
				.showImageForEmptyUri(resourceId).showImageOnFail(resourceId)
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.resetViewBeforeLoading(true)
				.displayer(new FadeInBitmapDisplayer(20)).build();
	}

	/**
	 * Display the passed-in image view with passed-in URL, image display
	 * options and image loading listener.
	 * 
	 * @param URL
	 * @param imageView
	 * @param displayImageOptions
	 * @param ImageLoadingListener
	 */
	public void displayImage(final String url, final ImageView imageView,
			final DisplayImageOptions displayImageOptions,
			final ImageLoadingListener imageLoadingListener) {
		mImageLoader.displayImage(url, imageView, displayImageOptions,
				imageLoadingListener);
	}

	@SuppressWarnings("deprecation")
	public Bitmap loadImageWithUri(final String uri) {
		return mImageLoader.getMemoryCache().get(uri);
	}

	// public boolean isImageCached(final ImageView imageView,
	// final String imageUrl) {
	// return mImageLoader.isBitmapCached(new ImageViewAware(imageView),
	// imageUrl);
	// }

}
